<template>
  <div id="app" @click="isDo">
    <a-config-provider :locale="zhCN"
      ><router-view v-if="isRouterAlive && !$route.meta.keepAlive" />
      <keep-alive>
        <router-view
          v-if="isRouterAlive && $route.meta.keepAlive"
        ></router-view>
      </keep-alive>
    </a-config-provider>
  </div>
</template>
<script>
import { getUserResourceList } from "@/api/Resource/resource";
import zhCN from "ant-design-vue/lib/locale-provider/zh_CN";
export default {
  provide() {
    return {
      reload: this.reload,
    };
  },
  data() {
    return {
      zhCN,
      isRouterAlive: true,
      lastTime: null, //最后一次点击的时间
      currentTime: null, //当前点击的时间
      timeOut: 30 * 60 * 1000, //设置超时时间：30分钟未操作
      Handler: "",
      beforeUnload: "",
    };
  },
  watch: {
    //监视路由
    $route: function (newValue, oldValue) {
      this.isDo();
    },
  },
  methods: {
    //获取菜单资源
    async getUserResourceList() {
      if (this.$route.path == "/KanBan") return;

      if (this.$route.path != "/login") {
        let res = await getUserResourceList();

        if (res.Status == 10000) {
          localStorage.setItem("usermenu", JSON.stringify(res.Data));
        }
      }
    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(() => {
        this.isRouterAlive = true;
      });
    },
    resizeBaseFontSize() {
      var rootHtml = document.documentElement;
      var rem = document.documentElement.clientWidth;
      var fontSize = (rem / 1920) * 16;
      rootHtml.style.fontSize = `${fontSize}px`;
    },
    isDo() {
      localStorage.setItem("functionName", this.$route.meta.title);

      if (this.$route.path == "/KanBan") return;

      if (this.$route.path != "/login") {
        this.currentTime = new Date().getTime(); //记录这次点击或操作键盘的时间
        if (this.currentTime - this.lastTime > this.timeOut) {
          this.$message.error("长时间未操作,请重新登录");
          localStorage.removeItem("nbcToken");
          localStorage.removeItem("nbcUserInfo");
          // localStorage.removeItem("nbcUsername");
          localStorage.removeItem("nbcPassword");

          //this.$store.commit("SynchronizeTabs", []);
          //this.$router.push("/login");
          this.$router.push({
            path: "/login",
            query: {
              redirect: true,
            },
          });
          this.lastTime = new Date().getTime();
          //判断上次最后一次点击的时间和这次点击的时间间隔是否大于10分钟
          // 这里写状态已过期后执行的操作
        } else {
          this.lastTime = new Date().getTime(); //如果在10分钟内点击，则把这次点击的时间记录覆盖掉之前存的最后一次点击的时间
        }
      } else {
        this.lastTime = new Date().getTime();
      }
    },
    // 浏览器页面关闭，清空登录信息
    beforeunloadHandler(e) {
      this.beforeUnload = new Date().getTime();
    },
    unloadHandler(e) {
      this.Handler = new Date().getTime() - this.beforeUnload;
      //两个事件执行间隔事件会少于5ms，是为关闭页面
      if (this.Handler <= 5) {
        //清空用户的信息的操作
        // localStorage.removeItem("nbcToken");
        // localStorage.removeItem("nbcUserInfo");
        // localStorage.removeItem("nbcUsername");
        // localStorage.removeItem("nbcPassword");
        // this.$store.commit("SynchronizeTabs", []);
      }
    },
  },
  created() {
    this.lastTime = new Date().getTime();
  },
  mounted() {
    window.addEventListener("keydown", this.isDo);
    window.addEventListener("beforeunload", (e) => this.beforeunloadHandler(e));
    window.addEventListener("unload", (e) => this.unloadHandler(e));
    this.isDo();
    this.getUserResourceList();
  },
  //在destroyed生命周期销毁事件
  destroyed() {
    window.removeEventListener("keydown", this.isDo);
    window.removeEventListener("beforeunload", (e) =>
      this.beforeunloadHandler(e)
    );
    window.removeEventListener("unload", (e) => this.unloadHandler(e));
  },
};
</script>
<style lang="less">
#app {
  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: 0.875rem;
  color: #303133;
  height: 100%;
  width: 100%;
}

/deep/ .ant-table-tbody > tr > td {
  padding: 0.75rem 0.625rem !important;
}
.ant-modal div[aria-hidden="true"] {
  display: none !important;
}
</style>
